home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / pilot-schlep.c < prev    next >
C/C++ Source or Header  |  1997-06-13  |  4KB  |  209 lines

  1. /* pilot-schlep.c:  Utility to transfer arbitrary data to/from your Pilot
  2.  *
  3.  * (c) 1996, Kenneth Albanowski.
  4.  *
  5.  * This is free software, licensed under the GNU Public License V2.
  6.  * See the file COPYING for details.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <signal.h>
  13. #include "pi-source.h"
  14. #include "pi-socket.h"
  15. #include "pi-file.h"
  16. #include "pi-dlp.h"
  17.  
  18. #define pi_mktag(c1,c2,c3,c4) (((c1)<<24)|((c2)<<16)|((c3)<<8)|(c4))
  19.  
  20. int sd = 0;
  21. char * device;
  22. char * progname;
  23.  
  24. RETSIGTYPE SigHandler(int signal);
  25.  
  26. void Connect(void) {
  27.   struct pi_sockaddr addr;
  28.   int ret;
  29.  
  30.   if (sd!=0)
  31.     return;
  32.     
  33.   signal(SIGHUP, SigHandler);
  34.   signal(SIGINT, SigHandler);
  35.   signal(SIGSEGV, SigHandler);
  36.       
  37.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  38.     perror("pi_socket");
  39.     exit(1);
  40.   }
  41.  
  42.   addr.pi_family = PI_AF_SLP;
  43.   strcpy(addr.pi_device,device);
  44.   
  45.   ret = pi_bind(sd, (struct sockaddr*)&addr, sizeof(addr));
  46.   if(ret == -1) {
  47.     perror("pi_bind");
  48.     exit(1);
  49.   }
  50.     
  51.   fprintf(stderr, "Waiting for connection (press the HotSync button now)...\n");
  52.  
  53.   ret = pi_listen(sd,1);
  54.   if(ret == -1) {
  55.     perror("pi_listen");
  56.     exit(1);
  57.   }
  58.  
  59.   sd = pi_accept(sd,0,0);
  60.   if(sd == -1) {
  61.     perror("pi_accept");
  62.     exit(1);
  63.   }
  64.  
  65.   fprintf(stderr, "Connected\n");
  66. }
  67.  
  68. void Disconnect(void)
  69. {
  70.   if(sd==0)
  71.     return;
  72.     
  73.   dlp_EndOfSync(sd, 0);
  74.   pi_close(sd);
  75.   sd = 0;
  76. }
  77.  
  78. RETSIGTYPE SigHandler(int signal)
  79. {
  80.   fprintf(stderr, "Abort on signal!\n");
  81.   Disconnect();
  82.   exit(3);
  83. }
  84.  
  85. void Delete(void)
  86. {
  87.   Connect();
  88.  
  89.   if (dlp_OpenConduit(sd)<0) {
  90.     fprintf(stderr, "Exiting on cancel, data not deleted.\n");
  91.     exit(1);
  92.   }
  93.  
  94.   dlp_DeleteDB(sd, 0, "Schlep");
  95.       
  96.   fprintf(stderr, "Delete done.\n");
  97. }
  98.  
  99. int segment = 4096;
  100.  
  101. void Install(void)
  102. {
  103.   unsigned long len;
  104.   int j;
  105.   int db;
  106.   int l;
  107.   char buffer[0xffff];
  108.   
  109.   if (isatty(fileno(stdin))) {
  110.     fprintf(stderr, "Cannot install from tty, please redirect from file.\n");
  111.     exit(1);
  112.   }
  113.   
  114.   Connect();
  115.  
  116.   if ( dlp_OpenConduit(sd) < 0) {
  117.     fprintf(stderr, "Exiting on cancel. Data not installed.\n");
  118.     exit(1);
  119.   }
  120.  
  121.   dlp_DeleteDB(sd, 0, "Schlep");
  122.   if (dlp_CreateDB(sd, pi_mktag('S','h','l','p'), pi_mktag('D','A','T','A'), 0, dlpDBFlagResource, 1, "Schlep", &db)<0)
  123.     return;
  124.   
  125.   fprintf(stderr,"Please wait, installing data");
  126.   
  127.   l = 0;
  128.   for(j=0;(len = read(fileno(stdin), buffer, segment)) > 0;j++) {
  129.     if (dlp_WriteResource(sd, db, pi_mktag('D','A','T','A'), j, buffer, len)<0)
  130.       break;
  131.     l+=len;
  132.     fprintf(stderr,".");
  133.   }
  134.   fprintf(stderr,"\n%d bytes written\n", l);
  135.   
  136.   dlp_CloseDB(sd, db);
  137. }
  138.  
  139. void Fetch(void)
  140. {
  141.   int db;
  142.   int i;
  143.   int l;
  144.   char buffer[0xffff];
  145.          
  146.   Connect();
  147.  
  148.   if (dlp_OpenConduit(sd)<0) {
  149.     fprintf(stderr, "Exiting on cancel, data not retrieved.\n");
  150.     exit(1);
  151.   }
  152.   
  153.   if (dlp_OpenDB(sd, 0, dlpOpenRead, "Schlep", &db)<0)
  154.     return;
  155.   
  156.   for (i=0;
  157.        (l = dlp_ReadResourceByType(sd, db, pi_mktag('D','A','T','A'), i, buffer, 0, 0)) > 0;
  158.        i++) {
  159.     write(fileno(stdout), buffer, l);
  160.   }
  161. }
  162.  
  163. void Help(void)
  164. {
  165.       fprintf(stderr,"Usage: %s %s command(s)\n\n",progname,TTYPrompt);
  166.       fprintf(stderr,"Where a command is one of: -i(nstall) < file \n");
  167.       fprintf(stderr,"                           -f(etch)   > file \n");
  168.       fprintf(stderr,"                           -d(elete)\n");
  169.       exit(0);
  170. }
  171.  
  172. int main(int argc, char *argv[])
  173. {
  174.   int c;
  175.   extern char* optarg;
  176.   extern int optind;
  177.  
  178.   progname = argv[0];
  179.  
  180.   if (argc < 3) {
  181.     Help();
  182.   }
  183.  
  184.   device = argv[1];
  185.   
  186.   optind = 2;
  187.   while ((c = getopt(argc, argv, "ifdh")) != -1) {
  188.     switch (c) {
  189.     case 'i':
  190.       Install();
  191.       break;
  192.     case 'f':
  193.       Fetch();
  194.       break;
  195.     case 'd':
  196.       Delete();
  197.       break;
  198.     default:
  199.     case 'h': case '?':
  200.       Help();
  201.     }
  202.   }
  203.   
  204.   Disconnect();
  205.   
  206.   exit(0);
  207. }
  208.  
  209.